package com.jjyu.controller;

import com.jjyu.entity.Restaurant;
import com.jjyu.entity.Result;
import com.jjyu.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jjyu.entity.Result.success;

/**
 * @author jiajunyu
 */
@RestController
@RequestMapping("/restaurants")
public class RestaurantController {
    @Autowired
    private RestaurantService restaurantService;

    /**
     * 新增 Restaurant
     */
    @PostMapping
    public Result addRestaurant(@RequestBody Restaurant restaurant) {
        System.out.println("addRestaurant");
        restaurantService.save(restaurant);
        return success();
    }

    /*
     * 获取所有 Restaurant
     */
    @GetMapping("/all")
    public Result<List<Restaurant>> getAllRestaurant() {
        System.out.println("getAllRestaurant");
        Result<List<Restaurant>> result = success(restaurantService.getAllRestaurant());
        return result;
    }

    @GetMapping("/search")
    public Result<List<Restaurant>> getRestaurantByAddStr(String addStr) {
        System.out.println("getRestaurantByAddStr");
        Result<List<Restaurant>> result = success(restaurantService.getRestaurantByAddStr(addStr));
        return result;
    }

    /**
     * 根据ID返回餐厅信息
     */
    @GetMapping("/{id}")
    public Result<Restaurant> getRestaurantById(@PathVariable("id") Integer id) {
        System.out.println("getRestaurantById");
        Result<Restaurant> result = success(restaurantService.getById(id));
        return result;
    }
    @PostMapping("/restaurants/create")
    public Result<Restaurant> add(@RequestBody Restaurant restaurant) {

            int x=restaurantService.addRestaurant(restaurant);
            if(x==1){
                return Result.success(restaurant);
            }else
                return Result.error("创建失败！");
        }

}

